﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class CateringMainForm : Form
    {
        private DeskInfoBLL _deskInfoBLL = new DeskInfoBLL();
        private CateringLoginForm loginForm;
        private RoomInfoBLL _roomInfoBLL = new RoomInfoBLL();
        public CateringMainForm(CateringLoginForm cateringLoginForm)
        {
            InitializeComponent();
            loginForm = cateringLoginForm;
        }

        private void CateringMainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            loginForm.Close();
        }

        private void btnMemmber_Click(object sender, EventArgs e)
        {
            //展示会员管理窗体
            MemberInfoForm memberInfoForm = new MemberInfoForm();
            memberInfoForm.ShowDialog();
        }


        //展示时间
        private void ShowTime()
        {
            DateTime dateTime = DateTime.Now;
            labTime.Text = dateTime.ToString("yyyy-MM--dd ddd hh:mm:ss");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ShowTime();
        }

        private void CateringMainForm_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();

            ShowRoomInfo();

            ShowRoomDesk();
        }

        private void ShowRoomInfo()
        {
            //加载包房 
            DataTable dataTable = _roomInfoBLL.SelectRoomInfo();
            
            foreach (DataRow dataRow in dataTable.Rows)
            {
                TabPage tabPage = new TabPage(dataRow[1].ToString());

                //tag属性为tabPage存储数据（包房的行数据）
                tabPage.Tag = dataRow;

                tabControl.TabPages.Add(tabPage);
            }
        }


       


        private void ShowRoomDesk()
        {
            
            //获取选中的标签页
            TabPage selectedTab = tabControl.SelectedTab;
            selectedTab.Controls.Clear();
            //获取标签页中Tag属性存储的数据
            DataRow dataRow = selectedTab.Tag as DataRow;

            //获取包房的Id
            int roomId = Convert.ToInt32(dataRow[0]);


            //展示桌子的图片和信息（ListView控件和imageList控件）
            DataTable deskInfo = _deskInfoBLL.SelectDeskInfoById(roomId);

            //展示桌子的图片和信息（ListView控件和imageList控件）
            //创建ListView对象
            ListView listView = new ListView();

            listView.MultiSelect = false;
            //调整ListView的大小
            listView.Dock = DockStyle.Fill;
            //将ListView添加到被选中的控件集合中
            selectedTab.Controls.Add(listView);

            //初始化ListView的数据
            listView.View = View.LargeIcon;
            listView.LargeImageList = imageList1;


            //获取所有行的桌子信息
            foreach (DataRow v in deskInfo.Rows)
            {
                //将获取的桌子添加到ListView中
                ListViewItem listViewItem = new ListViewItem(v[2].ToString());
                listViewItem.ImageIndex = Convert.ToInt32(v[4]);
                listViewItem.Tag = v;
                listView.Items.Add(listViewItem);


            }


        }

        private void btnCategory_Click(object sender, EventArgs e)
        {
            //展示商品窗口
            ProductsInfoForm productsInfoForm = new ProductsInfoForm();
            productsInfoForm.ShowDialog();
        }


        //切换标签事件
        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowRoomDesk();
        }


        public event EventHandler MyEvent;
        


        private void btnBiling_Click(object sender, EventArgs e)
        {
            //判断是否选中桌子
            //获取选中的标签页
            TabPage selectedTab = tabControl.SelectedTab;

            

            //获取选中标签页中选中的ListView中选中的Item
            ListView control = selectedTab.Controls[0] as ListView;

            int count = control.SelectedItems.Count;

            if (count == 0)
            {
                MessageBox.Show("请选择开单的餐桌");
                return;
            }

            //获取选中的第一个餐桌的数据
            ListViewItem listViewItem = control.SelectedItems[0];

            //获取餐桌的可用状态
            DataRow dataRow = listViewItem.Tag as DataRow;

            if (Convert.ToInt32(dataRow[4]) == 1)
            {
                MessageBox.Show("请选择空闲的餐桌");
                return;
            }

            //弹出窗体前传输需要的数据
            OrderDTO orderDTO = new OrderDTO();
            DataRow dataroom = selectedTab.Tag as DataRow;
            orderDTO.RoomId = Convert.ToInt32(dataroom[0]);
            orderDTO.RoomName = dataroom[1].ToString();
            orderDTO.DeskId = Convert.ToInt32(dataRow[0]);
            orderDTO.DeskName = dataRow[2].ToString();
            orderDTO.RoomMinConsumption = Convert.ToInt32(dataroom[2]);
            

            AddOrderForm addOrderForm = new AddOrderForm();
            //订阅事件
            
            MyEvent += addOrderForm.GetValue;
            MyEvent.Invoke(this, orderDTO);
            DialogResult dialogResult = addOrderForm.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                ShowRoomDesk();
            }
            MyEvent -= addOrderForm.GetValue;
        }

        private void btnMoney_Click(object sender, EventArgs e)
        {
            //判断是否选中餐桌,获取ListView控件
            ListView listView = tabControl.SelectedTab.Controls[0] as ListView;

            //获取ListView中选中的数量
            int count = listView.SelectedItems.Count;

            if(count > 0)
            {
                //进行增加消费,并获取需要的数据
                DataRow dataRow = listView.SelectedItems[0].Tag as DataRow;

                //判断桌子的状态
                if (Convert.ToInt32(dataRow[4]) == 0)
                {
                    MessageBox.Show("请选择下单后的餐桌");
                }
                else
                {
                    //弹出窗体前传输需要的数据
                    OrderDTO orderDTO = new OrderDTO();
                    DataRow dataRow1 = tabControl.SelectedTab.Tag as DataRow;
                    orderDTO.RoomId = Convert.ToInt32(dataRow1[0]);
                    orderDTO.RoomName = dataRow1[1].ToString();
                    orderDTO.DeskId = Convert.ToInt32(dataRow[0]);
                    orderDTO.DeskName = dataRow[2].ToString();
                    OrderInfoBLL orderInfoBLL = new OrderInfoBLL();
                    orderDTO.OrderId = orderInfoBLL.SelectOrderIdByDeskId(orderDTO.DeskId);

                    OrderAddProductsForm orderAddProductsForm = new OrderAddProductsForm();
                    //订阅并且执行事件
                    MyEvent += orderAddProductsForm.GetData;
                    MyEvent.Invoke(this, orderDTO);

                    //弹出窗口
                    orderAddProductsForm.ShowDialog();
                    MyEvent -= orderAddProductsForm.GetData;
                }

            }
            else
            {
                MessageBox.Show("请选择餐桌后再增加消费");
            }
        }

        private void btnJieZhang_Click(object sender, EventArgs e)
        {

            //判断是否选中餐桌,获取ListView控件
            ListView listView = tabControl.SelectedTab.Controls[0] as ListView;

            //获取ListView中选中的数量
            int count = listView.SelectedItems.Count;

            if (count > 0)
            {
                //进行增加消费,并获取需要的数据
                DataRow dataRow = listView.SelectedItems[0].Tag as DataRow;

                //判断桌子的状态
                if (Convert.ToInt32(dataRow[4]) == 0)
                {
                    MessageBox.Show("请选择下单后的餐桌");
                }
                else
                {
                    //弹出窗体前传输需要的数据
                    OrderDTO orderDTO = new OrderDTO();
                    DataRow dataRow1 = tabControl.SelectedTab.Tag as DataRow;
                    orderDTO.RoomId = Convert.ToInt32(dataRow1[0]);
                    orderDTO.RoomName = dataRow1[1].ToString();
                    orderDTO.DeskId = Convert.ToInt32(dataRow[0]);
                    orderDTO.DeskName = dataRow[2].ToString();
                    OrderInfoBLL orderInfoBLL = new OrderInfoBLL();
                    orderDTO.OrderId = orderInfoBLL.SelectOrderIdByDeskId(orderDTO.DeskId);

                    PayOrderForm payOrderForm = new PayOrderForm();
                    //订阅并且执行事件
                    MyEvent += payOrderForm.GetData;
                    MyEvent.Invoke(this, orderDTO);

                    //弹出窗口
                    DialogResult dialogResult = payOrderForm.ShowDialog();
                    if (dialogResult == DialogResult.OK)
                    {
                        ShowRoomDesk();
                    }

                    MyEvent -= payOrderForm.GetData;
                }

            }
            else
            {
                MessageBox.Show("请选择餐桌后再结账");
            }
        }
    }
}
